package com.example.friendfindertwo;

import java.text.DateFormat;
import java.util.Date;
import java.util.List;

import model.Event;
import model.Friend;

import com.example.friendfindertwo.R.color;
import com.example.friendfindertwo.application.StartApplication;
import com.example.friendfindertwo.data.helper.AbstractDAO;
import com.example.friendfindertwo.data.helper.CategoryDAO;
import com.friendfindertwo.control.EventListAdapter;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


public class DetailEventFragment extends Fragment{
	// Log tag
	public static final String TAG = "DetailsFragment";

	// Bundle keys
	private static final String KEY_SELECTED_INDEX = "selIndex";

	// State
	private int selectedIndex;

	private String data;
	// UI
	private TextView content;
	private TextView  textViewTitle;
	private TextView  textViewDescription;
	private TextView  textViewCategorie;
	private TextView  textViewPlaceName;
	private TextView  textViewStartDate;
	private TextView  textViewCreatorName;
	private TextView  textViewEndDate;
	private TextView textViewPeopleInEvent;
	private Button buttonJoin;
	private List<Event> mEventList;
	private List<Friend> mPeopleInEvent;
	private ParseDatabase mDatabase;
	
	/* (non-Javadoc)
	 * @see android.support.v4.app.Fragment#onActivityCreated(android.os.Bundle)
	 */

	//	public static DetailEventFragment newInstance(int selectedIndex)
	//	{
	//		DetailEventFragment result = new DetailEventFragment();
	//
	////		Bundle args = new Bundle();
	////		args.putInt(KEY_SELECTED_INDEX, selectedIndex);
	////		result.setArguments(args);
	//		
	//		return result;
	//	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mDatabase = new ParseDatabase();
		if(savedInstanceState != null){
			selectedIndex = savedInstanceState.getInt("key", -1);
		} else {
			Bundle args = getArguments();
			if(args != null){
				selectedIndex = args.getInt("key", -1);
			} else {
				selectedIndex = -1;
			}
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		//		Friend friend = new Friend("fuend");
		//		AbstractDAO.open(getActivity().getApplicationContext());
		//
		//		FriendDAO friendDao = new FriendDAO();
		//		
		//		friendDao.createFriend(friend.getFriendName());
		//		
		//		CategoryDAO cat = new CategoryDAO(getActivity().getApplicationContext());
		//		cat.createCategoryDummies();
		//
		//		dummies = new EventDAO();
		//		this.mEventList=dummies.getAllEvents();

		//		String text = "selectedIndex:"+selectedIndex;
		//
		//		text=this.mEventList.get(selectedIndex).toString();


//		String text = "selectedIndex:"+selectedIndex;
//
//		text=this.mEventList.get(selectedIndex).toString();
		mEventList = StartApplication.getAllEvents();
		Log.i("DetailEventFragmment.mPeopleInEvent", mEventList.get(selectedIndex).getObjectId());
		mPeopleInEvent=mDatabase.getPeopleInEvent(mEventList.get(selectedIndex).getObjectId());
		Log.i("DetailEventFragmment.mPeopleInEvent.toString", mPeopleInEvent.toString());

		View root =inflater.inflate(R.layout.detail_event_view,container, false);

		//content= (TextView)root.findViewById(R.id.textViewDetails);
		//content.setText(text);
		textViewTitle= (TextView)root.findViewById(R.id.textViewTitle);
		textViewDescription= (TextView)root.findViewById(R.id.textViewDescription);
		textViewCategorie= (TextView)root.findViewById(R.id.textViewCategorie);
		textViewPlaceName= (TextView)root.findViewById(R.id.textViewPlaceName);
		textViewStartDate= (TextView)root.findViewById(R.id.textViewStartDate);
		textViewCreatorName= (TextView)root.findViewById(R.id.textViewCreatorName);
		textViewEndDate= (TextView)root.findViewById(R.id.textViewEndDate);
		textViewPeopleInEvent = (TextView)root.findViewById(R.id.textViewPeopleInEvent);
		buttonJoin = (Button)root.findViewById(R.id.buttonJoin);

		for(Friend friend : mPeopleInEvent){
			//Log.i("", "")
			if(friend.getAccountName() == StartApplication.getCurrentUser().getAccountName()){
				Log.i("DetailEventActivity.testCurrentUser", "WORK");
				buttonJoin.setVisibility(View.INVISIBLE);

			}else 				Log.i("DetailEventActivity.testCurrentUser", "NOT WORK");

		}
		textViewTitle.setText( mEventList.get(selectedIndex).getTitle());
		textViewCategorie.setText(mEventList.get(selectedIndex).getCategorie());
		if(mEventList.get(selectedIndex).getCategorie().equals("Food")){
			((ImageView) root.findViewById(R.id.imageViewCategory)).setImageResource(R.drawable.icon_food);
//			textViewTitle.setBackgroundColor(color.akzent);
//			textViewCategorie.setBackgroundColor(color.akzent);
		}else if(mEventList.get(selectedIndex).getCategorie().equals("Party")){
			((ImageView) root.findViewById(R.id.imageViewCategory)).setImageResource(R.drawable.icon_party);
//			textViewTitle.setBackgroundColor(color.normal);
//			textViewCategorie.setBackgroundColor(color.normal);
		}else if(mEventList.get(selectedIndex).getCategorie().equals("Adventure")){
			((ImageView) root.findViewById(R.id.imageViewCategory)).setImageResource(R.drawable.icon_adventure);
//			textViewTitle.setBackgroundColor(color.submit);
//			textViewCategorie.setBackgroundColor(color.submit);
		}

		textViewDescription.setText( mEventList.get(selectedIndex).getDescription());
		
		textViewPlaceName.setText( mEventList.get(selectedIndex).getPlaceName());
		Date enddate = mEventList.get(selectedIndex).getStartDate();
		textViewStartDate.setText(DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT).format(enddate));
		textViewCreatorName.setText(mEventList.get(selectedIndex).getCreator().getAccountName());
		Date startdate = mEventList.get(selectedIndex).getEndDate();
		textViewEndDate.setText( DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT).format(startdate));
		String peopleInEvent="";
		if(mPeopleInEvent.size()>0){
		for(Friend friend:mPeopleInEvent){
			peopleInEvent=peopleInEvent+"\t"+"\t"+"\t"+friend.getAccountName()+"\n";
		}
		}else{
			peopleInEvent=peopleInEvent+"\t"+"\t"+"\t"+"No one join the event yet.";
		}
		textViewPeopleInEvent.setText(peopleInEvent);
		buttonJoin.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Log.i("DetailEventFragmment.onclick", mEventList.get(selectedIndex).toString());
				mDatabase.addInEvent(""+mEventList.get(selectedIndex).getObjectId());
				getActivity().finish();
				//int color = 0;
				//peopleInEvent="\t"+accountName+"\n";
				//buttonJoin.setBackgroundColor(color);
				//Change the color of the button if confirmed
			}			  

		});
		//		content = (TextView)root.findViewById(R.id.textViewDetails);
		//		content.setText("BLABLA TEST");
		//		Log.i("onCreateView", "end function");
		//		return root;
		return root;

	}






}
